package com.my.test.thread.lock;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Cache {

    static ReentrantReadWriteLock wrl = new ReentrantReadWriteLock();

    static Lock read = wrl.readLock();

    static Lock write = wrl.writeLock();

    static Map<String,Object> cacheMap = new HashMap<>();

    public static void main(String[] args) {



    }

    public static Object get(String key){
        read.lock();
        try {
            return cacheMap.get(key);
        } finally {
            read.unlock();
        }
    }

    public static Object put(String key,Object obj){
        write.lock();
        try {
            return cacheMap.put(key,obj);
        } finally {
            write.unlock();
        }
    }

    public static void clear(String key,Object obj){
        write.lock();
        try {
            cacheMap.clear();
        } finally {
            write.unlock();
        }
    }
}
